package org.study.redis.api.util.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.*;
import redis.clients.jedis.resps.ScanResult;

import java.util.List;
import java.util.Set;

/**
 * Redis 操作工具类：key
 * 参考 {@link redis.clients.jedis.commands.KeyCommands}
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class RedisKeyUtils {
    private RedisKeyUtils() {
        throw new AssertionError("不能实例化 RedisKeyUtils 工具类");
    }

    public static boolean exists(String key) {
        boolean exists;
        try (Jedis jedis = SysJedisPool.getResource()) {
            exists = jedis.exists(key);
        }
        return exists;
    }

    public static long exists(String... keys) {
        long exists;
        try (Jedis jedis = SysJedisPool.getResource()) {
            exists = jedis.exists(keys);
        }
        return exists;
    }

    public static long exists(List<String> keys) {
        long exists;
        try (Jedis jedis = SysJedisPool.getResource()) {
            exists = jedis.exists(keys.toArray(new String[keys.size()]));
        }
        return exists;
    }

    public static long persist(String key) {
        long persist;
        try (Jedis jedis = SysJedisPool.getResource()) {
            persist = jedis.persist(key);
        }
        return persist;
    }

    public static String type(String key) {
        String type;
        try (Jedis jedis = SysJedisPool.getResource()) {
            type = jedis.type(key);
        }
        return type;
    }

    public static long expire(String key, long seconds) {
        long expire;
        try (Jedis jedis = SysJedisPool.getResource()) {
            expire = jedis.expire(key, seconds);
        }
        return expire;
    }

    public static long pexpire(String key, long milliseconds) {
        long pexpire;
        try (Jedis jedis = SysJedisPool.getResource()) {
            pexpire = jedis.pexpire(key, milliseconds);
        }
        return pexpire;
    }

    public static long expireAt(String key, long unixTime) {
        long expireAt;
        try (Jedis jedis = SysJedisPool.getResource()) {
            expireAt = jedis.expireAt(key, unixTime);
        }
        return expireAt;
    }

    public static long pexpireAt(String key, long millisecondsTimestamp) {
        long pexpireAt;
        try (Jedis jedis = SysJedisPool.getResource()) {
            pexpireAt = jedis.pexpireAt(key, millisecondsTimestamp);
        }
        return pexpireAt;
    }

    public static long ttl(String key) {
        long ttl;
        try (Jedis jedis = SysJedisPool.getResource()) {
            ttl = jedis.ttl(key);
        }
        return ttl;
    }

    public static long pttl(String key) {
        long pttl;
        try (Jedis jedis = SysJedisPool.getResource()) {
            pttl = jedis.pttl(key);
        }
        return pttl;
    }

    /**
     * 更改 key 的最后访问时间。如果 key 不存在，则忽略它
     *
     * @param key
     * @return long
     */
    public static long touch(String key) {
        long touch;
        try (Jedis jedis = SysJedisPool.getResource()) {
            touch = jedis.touch(key);
        }
        return touch;
    }

    public static long touch(String... keys) {
        long touch;
        try (Jedis jedis = SysJedisPool.getResource()) {
            touch = jedis.touch(keys);
        }
        return touch;
    }

    public static long touch(List<String> keys) {
        long touch;
        try (Jedis jedis = SysJedisPool.getResource()) {
            touch = jedis.touch(keys.toArray(new String[keys.size()]));
        }
        return touch;
    }

    public static List<String> sort(String key) {
        List<String> sort;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sort = jedis.sort(key);
        }
        return sort;
    }

    public static long sort(String key, String dstkey) {
        long sort;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sort = jedis.sort(key, dstkey);
        }
        return sort;
    }

    public static List<String> sort(String key, SortingParams sortingParams) {
        List<String> sort;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sort = jedis.sort(key, sortingParams);
        }
        return sort;
    }

    public static long sort(String key, SortingParams sortingParams, String dstkey) {
        long sort;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sort = jedis.sort(key, sortingParams, dstkey);
        }
        return sort;
    }

    public static List<String> sortReadonly(String key, SortingParams sortingParams) {
        List<String> sort;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sort = jedis.sort(key, sortingParams);
        }
        return sort;
    }

    public static long del(String key) {
        long del;
        try (Jedis jedis = SysJedisPool.getResource()) {
            del = jedis.del(key);
        }
        return del;
    }

    public static long del(String... keys) {
        long del;
        try (Jedis jedis = SysJedisPool.getResource()) {
            del = jedis.del(keys);
        }
        return del;
    }

    public static long del(List<String> keys) {
        long del;
        try (Jedis jedis = SysJedisPool.getResource()) {
            del = jedis.del(keys.toArray(new String[keys.size()]));
        }
        return del;
    }

    public static long unlink(String key) {
        long unlink;
        try (Jedis jedis = SysJedisPool.getResource()) {
            unlink = jedis.unlink(key);
        }
        return unlink;
    }

    public static long unlink(String... keys) {
        long unlink;
        try (Jedis jedis = SysJedisPool.getResource()) {
            unlink = jedis.unlink(keys);
        }
        return unlink;
    }

    public static long unlink(List<String> keys) {
        long unlink;
        try (Jedis jedis = SysJedisPool.getResource()) {
            unlink = jedis.unlink(keys.toArray(new String[keys.size()]));
        }
        return unlink;
    }

    public static boolean copy(String srcKey, String dstKey, boolean replace) {
        boolean copy;
        try (Jedis jedis = SysJedisPool.getResource()) {
            copy = jedis.copy(srcKey, dstKey, replace);
        }
        return copy;
    }

    public static String rename(String oldkey, String newkey) {
        String rename;
        try (Jedis jedis = SysJedisPool.getResource()) {
            rename = jedis.rename(oldkey, newkey);
        }
        return rename;
    }

    public static long renamenx(String oldkey, String newkey) {
        long renamenx;
        try (Jedis jedis = SysJedisPool.getResource()) {
            renamenx = jedis.renamenx(oldkey, newkey);
        }
        return renamenx;
    }

    public static Long memoryUsage(String key) {
        Long memoryUsage = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            memoryUsage = jedis.memoryUsage(key);
        }
        return memoryUsage;
    }

    public static Long memoryUsage(String key, int samples) {
        Long memoryUsage = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            memoryUsage = jedis.memoryUsage(key, samples);
        }
        return memoryUsage;
    }

    public static Long objectRefcount(String key) {
        Long objectRefcount = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            objectRefcount = jedis.objectRefcount(key);
        }
        return objectRefcount;
    }

    public static String objectEncoding(String key) {
        String objectEncoding = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            objectEncoding = jedis.objectEncoding(key);
        }
        return objectEncoding;
    }

    public static Long objectIdletime(String key) {
        Long objectIdletime = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            objectIdletime = jedis.objectIdletime(key);
        }
        return objectIdletime;
    }

    public static Long objectFreq(String key) {
        Long objectFreq = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            objectFreq = jedis.objectFreq(key);
        }
        return objectFreq;
    }

    public static String migrate(String host, int port, String key, int timeout) {
        String migrate = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            migrate = jedis.migrate(host, port, key, timeout);
        }
        return migrate;
    }

    public static String migrate(String host, int port, int timeout, MigrateParams params, String... keys) {
        String migrate = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            migrate = jedis.migrate(host, port, timeout, params, keys);
        }
        return migrate;
    }

    public static String migrate(String host, int port, int timeout, MigrateParams params, List<String> keys) {
        String migrate = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            migrate = jedis.migrate(host, port, timeout, params, keys.toArray(new String[keys.size()]));
        }
        return migrate;
    }

    public static Set<String> keys(String pattern) {
        Set<String> keys = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            keys = jedis.keys(pattern);
        }
        return keys;
    }

    public static ScanResult<String> scan(String cursor) {
        ScanResult<String> scan = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            scan = jedis.scan(cursor);
        }
        return scan;
    }

    public static ScanResult<String> scan(String cursor, ScanParams params) {
        ScanResult<String> scan = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            scan = jedis.scan(cursor, params);
        }
        return scan;
    }

    public static ScanResult<String> scan(String cursor, ScanParams params, String type) {
        ScanResult<String> scan = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            scan = jedis.scan(cursor, params, type);
        }
        return scan;
    }

    public static String randomKey() {
        String randomKey = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            randomKey = jedis.randomKey();
        }
        return randomKey;
    }
}
